<template>
  <div class="update-gender">
    <van-picker
      title="性别"
      show-toolbar
      :default-index="value"
      :columns="columns"
      @confirm="onConfirm"
      @change="onChange"
      @cancel="$emit('close')"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateGender',
  components: {},
  props: {
    value: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      columns: ['男', '女'],
      localGender: this.value
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onConfirm() {
      this.$toast.loading({
        message: '修改中，请稍后... ...',
        forbidClick: true,
        duration: 0
      })
      try {
        const localGender = this.localGender
        await updateUserProfile({ gender: localGender })
        // 更新视图
        this.$emit('input', localGender)
        // 关闭弹窗
        this.$emit('close')
        // 提示成功
        this.$toast('修改成功！')
      } catch (error) {
        this.$toast('更新失败，请稍后重试！')
      }
    },
    onChange(picker, value, index) {
      this.localGender = index
    }
  }
}
</script>

<style scoped lang="less"></style>
